package com.cxs.service.impl;

import com.cxs.client.OutInterfaceClient;
import com.cxs.client.resp.AccessTokenResp;
import com.cxs.client.resp.BaiduWeatherResp;
import com.cxs.client.resp.CaiHongPiResp;
import com.cxs.config.CommonConfig;
import com.cxs.service.WechatService;
import com.cxs.vo.Result;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Project: wechat-push
 * @Author: cxs2014501@163.com
 * @Create: 2023/9/7 10:01
 * @Description:
 **/
@Slf4j
@Service
public class WechatServiceImpl implements WechatService {

    @Autowired
    private OutInterfaceClient outInterfaceClient;

    @Autowired
    private CommonConfig commonConfig;

    @Override
    public Result pushEveryMsg() {
        Result r = Result.ok(0, "操作成功", null);
        try {
            // 通过百天气获取当日天气数据
            BaiduWeatherResp weatherInfo = outInterfaceClient.getWeatherInfo();
            if (0 != weatherInfo.getStatus()) {
                throw new RuntimeException("获取天气数据失败");
            }
            // 通过彩虹屁接口获取祝福语
            CaiHongPiResp caiHongPiInfo = outInterfaceClient.getCaiHongPiInfo();
            if (200 != caiHongPiInfo.getCode()) {
                throw new RuntimeException("获取彩虹屁数据失败");
            }
            // 获取微信平台access_token
            AccessTokenResp weChatAccessToken = outInterfaceClient.getWeChatAccessToken();
            if (null == weChatAccessToken || !StringUtils.hasLength(weChatAccessToken.getAccess_token())) {
                throw new RuntimeException("获取access_token失败");
            }
            BaiduWeatherResp.ResultVO weatherInfoResult = weatherInfo.getResult();
            BaiduWeatherResp.ResultVO.ForecastsVO forecastsVO = weatherInfoResult.getForecasts().get(0);

            CaiHongPiResp.ResultVO caiHongPiInfoResult = caiHongPiInfo.getResult();
            //1，配置
            WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
            wxStorage.setAppId(commonConfig.getAppId());
            wxStorage.setSecret(commonConfig.getSecret());
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxStorage);
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(commonConfig.getTouser())
                    .templateId(commonConfig.getTemplateId())
                    .build();
            //3,如果是正式版发送模版消息，这里需要配置你的信息
            templateMessage.addData(new WxMpTemplateData("riqi",forecastsVO.getDate() + "  "+ forecastsVO.getWeek(),"#00BFFF"));
            templateMessage.addData(new WxMpTemplateData("tiani",forecastsVO.getText_day(),"#00FFFF"));
            templateMessage.addData(new WxMpTemplateData("low",forecastsVO.getLow() + "","#173177"));
            templateMessage.addData(new WxMpTemplateData("high",forecastsVO.getHigh()+ "","#FF6347" ));
            templateMessage.addData(new WxMpTemplateData("caihongpi",caiHongPiInfoResult.getContent(),"#FF69B4"));
            templateMessage.addData(new WxMpTemplateData("lianai","-365","#FF1493"));
            templateMessage.addData(new WxMpTemplateData("shengri","120","#FFA500"));
            String beizhu = "宝贝";
            templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            e.printStackTrace();
            r.setMsg(e.getMessage());
            r.setCode(-1);
        }

        return r;
    }
}
